package com.sczmgk.track.activity.trackactivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.sczmgk.track.R;
import com.sczmgk.track.base.CheckPermissionBaseActivity;
import com.sczmgk.track.constant.Constant;
import com.sczmgk.track.db.impl.UserDaoImpl;
import com.sczmgk.track.dialog.DialogFactory;
import com.sczmgk.track.dialog.MessageDialog;
import com.sczmgk.track.entity.User;
import com.sczmgk.track.httpUtils.MyHttp;
import com.sczmgk.track.test.MyCallback;
import com.sczmgk.track.utils.CacheUtils;
import com.sczmgk.track.utils.MapUtil;
import com.sczmgk.track.utils.MyLog;
import com.sczmgk.track.utils.MyToast;
import com.sczmgk.track.utils.Tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sczmgk.track.MyApp.currPoi;
import static com.sczmgk.track.entity.CurrentLocation.latitude;
import static com.sczmgk.track.entity.CurrentLocation.longitude;

/**
 * Created by Administrator on 2017/9/27.
 * 检索
 */
public class SignActivity extends CheckPermissionBaseActivity {
    private static final int REFRESH_UI = 123;
    public PoiSearch mPoiSearch;
    private MapUtil mapUtil;
    private LinearLayout mSignBtn;
    private TextView mSignTv;
    private TextView mSignTimeTv;
    private TextView mSignName;
    private TextView mSignAddress;
    private TextView mAdjustTv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        isShowLeftView(false);

        setTitle(R.string.sign_title);
        intiView();
        setListener();
        mapUtil = new MapUtil();

        mapUtil.init((MapView) findViewById(R.id.tracing_mapView));
        User loginUser = UserDaoImpl.getLoginUser(SignActivity.this);
        String imagePath = null;
        if(loginUser != null){
            imagePath = loginUser.imageLocalPath;
        }
        mapUtil.addMyMarker(new LatLng(latitude, longitude), SignActivity.this, imagePath);
        mapUtil.animateMapStatus(new LatLng(latitude, longitude), 15.0f);
        mapUtil.getAddress(new LatLng(latitude, longitude), new MapUtil.OnGetGeoCallback() {
            @Override
            public void onSuccess(String address) {

            }

            @Override
            public void onFail() {

            }

            @Override
            public void onSuccess(PoiInfo poi) {
                MyLog.d(TAG, "进入签到界面,根据坐标取位置信息: " + poi.address);
                currPoi = poi;
                mSignBtn.setClickable(true);
                mSignTv.setText("签到");
                //TODO 设置地址,公司名
                mSignName.setText(currPoi.name);
                mSignAddress.setText(currPoi.address);
            }

            @Override
            public void onSuccess(List<PoiInfo> poiinfs) {

            }
        });

    }

    private void setListener() {
        mSignBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageDialog messageDialog = DialogFactory.chooseDialog(SignActivity.this, "签到时间: " + Tools.getTime() + "  签到地址: " + currPoi.address, "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        User loginUser = UserDaoImpl.getLoginUser(SignActivity.this);
                        if (loginUser.imeistatus == User.SIGN_ENABLE) {
                            showLoadingDiaLog();
                            //TODO 测试提交签到
                            Map<String, Object> dataMap = new HashMap<String, Object>();
                            dataMap.put("userid", CacheUtils.getLoginUserid(SignActivity.this));
                            dataMap.put("signaddress", (currPoi == null || Tools.isEmpty(currPoi.address) ? "测试写死的地址" : currPoi.address));
                            dataMap.put("companyname", (currPoi == null || Tools.isEmpty(currPoi.name) ? "测试写死的名字" : currPoi.name));
                            dataMap.put("remarkdes", "这是一个备注");
                            dataMap.put("maplong", (currPoi == null || currPoi.location == null ? longitude : currPoi.location.longitude));
                            dataMap.put("maplat", (currPoi == null || currPoi.location == null ? latitude : currPoi.location.latitude));

                            MyHttp.requestDataT(SignActivity.this, Constant.SIGN_URL, MyHttp.POST, null, dataMap, null, false, null, new MyCallback() {

                                @Override
                                public void onSuccess(Object o) {

                                    dismissLoadingDiaLog();
                                }

                                @Override
                                public void onUnknownError() {
                                    dismissLoadingDiaLog();
                                }

                                @Override
                                public void onError(String msg) {
                                    dismissLoadingDiaLog();
                                    showErrorMsg(msg);
                                }

                                @Override
                                public void onSuccess() {
                                    dismissLoadingDiaLog();
                                    MyToast.show(SignActivity.this, "签到成功");
                                }
                            });
                        } else {
                            DialogFactory.warningDialog(SignActivity.this, "你不能用此手机签到");
                        }
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                messageDialog.setTitle("提示");
                messageDialog.show();
            }
        });
    }

    private void intiView() {
        mSignBtn = (LinearLayout) findViewById(R.id.bt_sign);
        mSignTv = (TextView) findViewById(R.id.tv_sign);
        mSignTimeTv = (TextView) findViewById(R.id.tv_sign_time);
        mSignName = (TextView)findViewById(R.id.tv_sign_name);
        mSignAddress = (TextView) findViewById(R.id.tv_sign_address);

        mSignBtn.setClickable(false);
        mSignTv.setText("正在获取位置信息");
        mSignTimeTv.setText(Tools.getTime());
        mAdjustTv = (TextView) findViewById(R.id.tv_adjust);
        mAdjustTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SignActivity.this, TracingSearchActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }


    @Override
    public void onSelectedPhonePermission(String phoneNumber) {

    }

    @Override
    public void onSelectedStroage(int position) {

    }

    @Override
    public void onSelectCamera(int position) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

    private class ViewHolder {
        TextView tvAddress;
        TextView tvName;
    }

    @Override
    public void handleMessage(Message msg) {

    }

    /**
     * 附近检索
     */
    public void nearbySearch(String keyword, int page) {
        PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption();
        nearbySearchOption.location(new LatLng(latitude, longitude));
        nearbySearchOption.keyword(keyword);
        nearbySearchOption.radius(500);// 检索半径，单位是米
        nearbySearchOption.pageNum(page);
        nearbySearchOption.sortType(PoiSortType.distance_from_near_to_far);
        mPoiSearch.searchNearby(nearbySearchOption);// 发起附近检索请求
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapUtil.clear();
        if (mPoiSearch != null) {
            mPoiSearch.destroy();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1:
                    User loginUser = UserDaoImpl.getLoginUser(SignActivity.this);
                    String imagePath="";
                    if(loginUser != null){
                        imagePath = loginUser.imageLocalPath;

                    }
                    mapUtil.addMyMarker(currPoi.location, SignActivity.this, imagePath);
                    mapUtil.animateMapStatus(currPoi.location, 15.0f);
                    //TODO 设置选中的地址
                    mSignName.setText(currPoi.name);
                    mSignAddress.setText(currPoi.address);
                    break;
            }
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        mapUtil.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapUtil.onResume();
    }
}
